package com.test.platform.service.XTSReport;

import com.test.platform.dao.XTSReportMapper;
import com.test.platform.model.XTSReport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class XTSReportService {
    
    private static final Logger logger = LoggerFactory.getLogger(XTSReportService.class);
    
    @Autowired
    private XTSReportMapper xtsReportMapper;
    
    public List<XTSReport> getAllXTSReports() {
        try {
            return xtsReportMapper.findAll();
        } catch (Exception e) {
            logger.error("获取所有XTS报告失败", e);
            return new ArrayList<>();
        }
    }
    
    public XTSReport getXTSReportById(Integer id) {
        try {
            if (id == null) {
                return null;
            }
            return xtsReportMapper.findById(id);
        } catch (Exception e) {
            logger.error("获取XTS报告失败: id={}", id, e);
            return null;
        }
    }
    
    public List<XTSReport> getXTSReportsByOwnerAndName(String owner, String name) {
        try {
            return xtsReportMapper.findByOwnerAndName(owner, name);
        } catch (Exception e) {
            logger.error("获取XTS报告失败: owner={}, name={}", owner, name, e);
            return new ArrayList<>();
        }
    }
    
    public boolean createXTSReport(XTSReport xtsReport) {
        try {
            if (xtsReport == null) {
                return false;
            }
            xtsReport.setCreatedAt(LocalDateTime.now());
            xtsReport.setUpdatedAt(LocalDateTime.now());
            return xtsReportMapper.insert(xtsReport) > 0;
        } catch (Exception e) {
            logger.error("创建XTS报告失败", e);
            return false;
        }
    }
    
    public boolean updateXTSReport(XTSReport xtsReport) {
        try {
            if (xtsReport == null || xtsReport.getId() == null) {
                return false;
            }
            xtsReport.setUpdatedAt(LocalDateTime.now());
            return xtsReportMapper.update(xtsReport) > 0;
        } catch (Exception e) {
            logger.error("更新XTS报告失败: id={}", xtsReport.getId(), e);
            return false;
        }
    }
    
    public boolean deleteXTSReport(Integer id) {
        try {
            if (id == null) {
                return false;
            }
            return xtsReportMapper.deleteById(id) > 0;
        } catch (Exception e) {
            logger.error("删除XTS报告失败: id={}", id, e);
            return false;
        }
    }
}